package com.cardone.platform.common.controller;

import java.util.List;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.common.dto.OperateLogDto;
import com.cardone.platform.common.service.OperateLogService;

/**
 * 操作日志
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/common/operateLog/")
@Controller
public class OperateLogController {
	/**
	 * 初始化数据绑定
	 *
	 * @param webDataBinder
	 *          数据绑定
	 */
	// @InitBinder
	// @Deprecated
	// public void initBinder(final WebDataBinder webDataBinder) {
	// }

	/**
	 * 操作日志：删除
	 *
	 * @param deleteOperateLog
	 *          操作日志对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Callable<ReturnDataDto<String>> deleteJson(final OperateLogDto deleteOperateLog) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(OperateLogService.class).deleteByIds(deleteOperateLog);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, OperateLogController.log);
				}
			}
		};
	}

	/**
	 * 操作日志：分页查询
	 *
	 * @param findOperateLog
	 *          操作日志对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
	public void editHtml(final OperateLogDto findOperateLog, final Model model) {
		final OperateLogDto operateLog = ContextHolder.getBean(OperateLogService.class).findById(OperateLogDto.class, findOperateLog);

		model.addAttribute("operateLog", operateLog);
	}

	/**
	 * 操作日志：编辑
	 *
	 * @param findOperateLog
	 *          操作日志对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<OperateLogDto>> findByIdJson(final OperateLogDto findOperateLog, final Model model) {
		return new Callable<ReturnDataDto<OperateLogDto>>() {
			@Override
			public ReturnDataDto<OperateLogDto> call() throws Exception {
				try {
					final OperateLogDto operateLog = ContextHolder.getBean(OperateLogService.class).findById(OperateLogDto.class, findOperateLog);

					return ReturnDataUtils.newReturnData(operateLog);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, OperateLogController.log);
				}
			}
		};
	}

	/**
	 * 操作日志：查询
	 *
	 * @param findListOperateLog
	 *          操作日志对象
	 * @return 操作日志对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<List<OperateLogDto>>> findListByLikeCodeJson(final OperateLogDto findListOperateLog) {
		return new Callable<ReturnDataDto<List<OperateLogDto>>>() {
			@Override
			public ReturnDataDto<List<OperateLogDto>> call() throws Exception {
				try {
					final List<OperateLogDto> operateLogList = ContextHolder.getBean(OperateLogService.class).findListByLikeCode(OperateLogDto.class, findListOperateLog);

					return ReturnDataUtils.newReturnData(operateLogList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, OperateLogController.log);
				}
			}
		};
	}

	/**
	 * 操作日志：分页查询
	 *
	 * @param paginationOperateLog
	 *          操作日志对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<PaginationDto<OperateLogDto>>> paginationByLikeCodeJson(final OperateLogDto paginationOperateLog) {
		return new Callable<ReturnDataDto<PaginationDto<OperateLogDto>>>() {
			@Override
			public ReturnDataDto<PaginationDto<OperateLogDto>> call() throws Exception {
				try {
					final PaginationDto<OperateLogDto> operateLogPagination = ContextHolder.getBean(OperateLogService.class).paginationByLikeCode(OperateLogDto.class, paginationOperateLog);

					return ReturnDataUtils.newReturnData(operateLogPagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, OperateLogController.log);
				}
			}
		};
	}

	/**
	 * 操作日志：分页查询
	 *
	 * @param paginationOperateLog
	 *          操作日志对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
	public void paginationHtml(final OperateLogDto paginationOperateLog, final Model model) {
		final PaginationDto<OperateLogDto> operateLogPagination = ContextHolder.getBean(OperateLogService.class).paginationByLikeCode(OperateLogDto.class, paginationOperateLog);

		model.addAttribute("operateLogPagination", operateLogPagination);
	}

	/**
	 * 操作日志：保存
	 *
	 * @param saveOperateLog
	 *          操作日志对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
	public Callable<ReturnDataDto<String>> saveJson(final OperateLogDto saveOperateLog) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(OperateLogService.class).saveByIdOrCode(OperateLogDto.class, saveOperateLog);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, OperateLogController.log);
				}
			}
		};
	}
}
